﻿using CSharp大作业.Gobang.GameComponet.Store;
using Game;
using System;
using System.IO;
using System.Windows.Forms;

namespace CSharp大作业.Gobang.Game
{
    public partial class ReProduce : Form
    {
        private DB db;
        public static int index = 0;
        public int id_game = 0;
        public ReProduce(int id_game)
        {
            InitializeComponent();
            this.id_game = id_game;
            db = new DB();
            //删除数据库所有空数据对局
            db.deleteNullGame(id_game);
            db.ReadGame();
            initial();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void ReProduce_Load(object sender, EventArgs e)
        {
        }
        //将数据库所有对局数据显示
        private void initial()
        {
            listView1.Columns.Add("序号", 40);
            listView1.Columns.Add("名称", 100);
            listView1.Columns.Add("游戏时间", 120);
            int i = 0;
            while (db.games[i] != null)
            {
                ListViewItem item = new ListViewItem(db.games[i].id_game.ToString());
                item.SubItems.Add(db.games[i].name);
                item.SubItems.Add(db.games[i].time);
                listView1.Items.Add(item);
                i++;
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            ListViewItem item;
            if (!String.IsNullOrWhiteSpace(textBox1.Text) && db.games[0] != null)
            {
                listView1.Clear();
                listView1.Columns.Add("序号", 40);
                listView1.Columns.Add("名称", 100);
                listView1.Columns.Add("游戏时间", 120);
                db.getGame(textBox1.Text);
                int i = 0;
                while (db.games[i] != null)
                {
                    item = new ListViewItem(db.games[i].id_game.ToString());
                    item.SubItems.Add(db.games[i].name);
                    item.SubItems.Add(db.games[i].time);
                    listView1.Items.Add(item);
                    i++;
                }
            }
            listView1.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            db.deleteAll();
            GobangGame.DeleteAllFiles();
            listView1.Items.Clear();
            initial();
            listView1.Refresh();
        }

        private void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            try
            {
                index = Convert.ToInt32(listView1.SelectedItems[0].Text);
            }
            catch (Exception ex)
            {

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //传值给Game窗体，调用board.Reproduce函数进行复现
            DialogResult = DialogResult.OK;
            global::Game.GobangGame.index = index;
            this.Close();
        }

        //删除指定对局
        private void button4_Click(object sender, EventArgs e)
        {
            /* int i = listView1.SelectedItems.Count;
             for (int j = 0; j < i; j++)
             {
                 index= Convert.ToInt32(listView1.SelectedItems[j].Text);
                 //如果选中的和当前对局不同才可删除
                 if (index != id_game)
                 {
                     db.deleteGame(index);
                     db.ReadGame();
                     listView1.Items.Clear();
                     initial();
                     listView1.Refresh();
                 }
                 else MessageBox.Show("禁止删除当前对局信息！");
             }*/
            if (index != id_game)
            {
                db.deleteGame(index);
                GobangGame.DeleteOneFile(index);
                db.ReadGame();
                listView1.Items.Clear();
                initial();
                listView1.Refresh();
            }
            else MessageBox.Show("禁止删除当前对局信息！");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = Path.Combine(Environment.CurrentDirectory, "Pictures\\五子棋");
            openFileDialog1.Filter = "Image files (*.jpg, *.png)|*.jpg;*.png";
            openFileDialog1.Title = "选择对局截图";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string FileName = openFileDialog1.SafeFileName;
                string id_game = "";
                string name = "";
                int i = 0;
                while (!FileName[i].Equals('_'))
                {
                    id_game += FileName[i];
                    i++;
                }
                i++;
                while (i < FileName.Length && !FileName[i].Equals('.'))
                {
                    name += FileName[i];
                    i++;
                }
                index = Convert.ToInt32(id_game);
                textBox1.Text = name;
                textBox1.Refresh();
            }
        }
    }
}
